<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('标题')" prop="title">
            <el-input v-model="form.title" :placeholder="$t('标题')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('发布时间')" prop="submitTime">
            <DatePicker v-model="form.submitTime" :placeholder="$t('发布时间')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('原告')" prop="plaintiffs">
            <el-input v-model="form.plaintiffs" :placeholder="$t('原告')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('被告')" prop="defendants">
            <el-input v-model="form.defendants" :placeholder="$t('被告')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('法院')" prop="court">
            <el-input v-model="form.court" :placeholder="$t('法院')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('案号')" prop="caseNo">
            <el-input v-model="form.caseNo" :placeholder="$t('案号')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('原文链接地址')" prop="urlAdd">
            <div style="margin-top: 15px;">
              <el-input v-model="form.urlAdd" show-word-limit maxlength="250" :placeholder="$t('原文链接地址')">
                <el-button slot="append" icon="el-icon-search" @click="openUrl(form.urlAdd)">{{ $t('访问') }}</el-button>
              </el-input>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('案由')" prop="caseReason">
            <el-input v-model="form.caseReason" type="textarea" :placeholder="$t('案由')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div v-show="!btnHide" slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <SaveBtn has-permi="company:action" />
    </div>
  </div>
</template>

<script>
    import { saveCompanyLegalAction, getCompanyLegalAction } from '@/api/company/action/companyLegalAction'
    export default {
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  title: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  submitDate: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
                },
                // 是否隐藏保存等按钮
                btnHide: false
            }
        },
        created() {
        },
        mounted() {
          this.$bus.$emit('async_done')
        },
        methods: {
            // 初始化数据
            init(data) {
              this.loading = true
                if (data != null && data.id !== undefined) {
                    getCompanyLegalAction(data.id).then(response => {
                        this.form = response.data
                      this.loading = false
                    })
                } else {
                    this.reset()
                  this.loading = false
                }
                this.form.companyId = this.$route.query.companyId
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                companyId: undefined,
                title: undefined,
                submitTime: undefined,
                plaintiffs: undefined,
                defendants: undefined,
                court: undefined,
                caseNo: undefined,
                urlAdd: undefined,
                caseReason: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                delFlag: undefined,
                remark: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        this.form.source = '1'
                        this.form.companyId = this.$route.query.companyId
                        saveCompanyLegalAction(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                              if (this.$route.query.companyId) {
                                this.$store.dispatch('bussRisk/UPDATE_BUSS_RISK_COUNT', { id: this.$route.query.companyId })
                              }
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                              this.loading = false
                            }
                        }).catch(() => {
                          this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
            },
            openUrl(url) {
              console.log(url)
              if (url.indexOf('http://') !== -1) {
                window.open(url)
              } else if (url.indexOf('https://') !== -1) {
                window.open(url)
              } else {
                window.open('http://' + url)
              }
            }

        }

    }
</script>
